/*
 * Copyright (C) 2010 by CODEBISTRO LLC.
 *
 * The license is granted to use, reproduce, modify, display, perform, sublicense
 * and distribute this code under the conditions of the Mozilla Public License v 1.1.
 *
 * The text of the license is available with this source as MPL-1.1.txt
 * or at http://www.mozilla.org/MPL/MPL-1.1.txt.
 */

package org.codebistro.tools;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Generate build number based on the svn version information.
 *
 * @goal build-number
 * @phase generate-resources
 */
public class BuildNumberMojo extends AbstractMojo {
    /**
     * Location of the file.
     *
     * @parameter expression="${buildnumber.outputPath}"
     */
    private File outputPath;

    /**
     * Project build directory. Only used if path is not set
     *
     * @parameter expression="${project.build.directory}"
     */
    private File buildDirectory;

    /**
     * Project root directory. 
     *
     * @parameter expression="${basedir}"
     */
    private File basedir;

    BuildNumber rep= new BuildNumber();

    public void execute() throws MojoExecutionException, MojoFailureException {
        if (outputPath == null)
            outputPath = new File(new File(buildDirectory, "generated-resources"), "buildnumber.properties");
        rep.setOutputPath(outputPath);
        rep.setBuildDirectory(buildDirectory);
        rep.setBasedir(basedir);
        
        getLog().info("buildnumber.outputPath=" + outputPath);
        try {
            String result= rep.execute(true);
            getLog().info("\tbuild.number=" + result);            
        } catch(Exception ex) {
            throw new MojoExecutionException("Exception: ", ex);
        }
    }
}
